package com.nogroup.springbootproject.controller;

import com.nogroup.springbootproject.dto.CourseDto;
import com.nogroup.springbootproject.dto.StudentDto;
import com.nogroup.springbootproject.entity.Course;
import com.nogroup.springbootproject.entity.Student;
import com.nogroup.springbootproject.service.StudentService;
import com.nogroup.springbootproject.utils.PageUtils;
import com.nogroup.springbootproject.utils.result.DataResult;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.List;

/**
 * (Student)表控制层
 *
 * @author makejava
 * @since 2024-04-07 18:45:33
 */
@RestController
@RequestMapping("student")
public class StudentController {
    /**
     * 服务对象
     */
    @Resource
    private StudentService studentService;

    /**
     * 分页查询
     *
     * @param student 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Student>> queryByPage(Student student, PageRequest pageRequest) {
        return ResponseEntity.ok(this.studentService.queryByPage(student, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Student> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.studentService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param student 实体
     * @return 新增结果
     */
    @PostMapping
    @RequestMapping("studentInsert")
    public DataResult add(Student student) {
        return DataResult.successByData(this.studentService.insert(student));
    }


    /**
     * 删除一条学生数据
     * @param student
     * @return
     */
    @PostMapping
    @RequestMapping("deleteById")
    public DataResult deleteById(Student student){
        return DataResult.successByData(this.studentService.deleteById(student));
    }

    /**
     * 编辑数据
     *
     * @param student 实体
     * @return 编辑结果
     */
    @PutMapping
    @RequestMapping("edit")
    public DataResult edit(Student student) {
        return DataResult.successByData(this.studentService.update(student));
    }



    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("getStudentByNameList")
    public DataResult getStudentByNameList(@RequestBody StudentDto studentDto){
        studentDto.setPage(PageUtils.getStartPage(studentDto.getPage(),studentDto.getLimit()));
        List<Student> students = studentService.getStudentByNameList(studentDto);

        long count = studentService.countPage(studentDto);
        return DataResult.successByTotalData(students,count);
    }

    @RequestMapping("getAllStudents")
    public DataResult getAllStudents(){
        System.out.println("12345");
        List<Student> students = studentService.getAllStudents();
        System.out.println("输出students内容：" + students);
        return DataResult.successByDatas(students);
    }

    @RequestMapping("getStudentsByFuzzySearch")
    public DataResult getStudentsByFuzzySearch(StudentDto studentDto){
//        System.out.println("studentDtoGetPage:" + studentDto.getPage() + "     studentDtoGetLimit:" + studentDto.getLimit());
        long count = studentService.countPage(studentDto);
        int index = PageUtils.getStartPage(studentDto.getPage(), studentDto.getLimit());
        studentDto.setPage(index < count ? index : 0);
//        System.out.println("studentDtoGetPage:" + studentDto.getPage() + "     studentDtoGetLimit:" + studentDto.getLimit());
        List<Student> students = studentService.getStudentsByFuzzySearch(studentDto);
        students.forEach(student -> {
            System.out.println("studentAddress:" + student.getStudentAddress());
        });
        return DataResult.successByTotalData(students,count);
    }
}

